package cardgame.demo.event;

import cardgame.demo.logger.LogUtil;
import org.springframework.util.StringUtils;

import java.util.Scanner;

/**
 * 卡牌事件模板类
 */
public abstract class CardEvent extends BaseEvent{
    protected String userId;
    protected String cardName;
    protected boolean available = true;
    protected String targetId;
    public void execute(){
        mentionUse();
        targetChosenEvent();
        beforeEffective();
        if(available){
            cardEffective();
        }
    }

    /**
     * 卡牌生效前
     */
    private void beforeEffective() {
        LogUtil.println("%s的卡牌【%s】即将生效", userId, cardName);
        String card = getInputString();
        if("识破".equals(card)){
            new ShiPoCardEvent()
                    .targetCardEvent(this)
                    .userId("player2")
                    .execute();
        }
    }

    private CardEvent waitForCardEvent(){
        return this;
    }

    private String getInputString(){
        Scanner scanner = new Scanner(System.in);
        LogUtil.println("请打出要响应的牌：");
        return scanner.nextLine();
    }

    /**
     * 指定目标时
     */
    private void targetChosenEvent() {
        if(!StringUtils.hasLength(targetId)){
            return;
        }
        LogUtil.println("%s使用牌【%s】指定了%s为目标", userId, cardName, targetId);
        if("离间".equals(getCardName())){
            LogUtil.println("离间无法响应离间");
            return;
        }
        String card = getInputString();
        if("离间".equals(card)){

            new LiJianCardEvent()
                    .targetCardEvent(this)
                    .userId("player2")
                    .targetId("player3")
                    .execute();
        }
    }

    /**
     * 声明使用
     */
    private void mentionUse() {
        LogUtil.println("%s使用了%s",userId, cardName);
    }
    /**
     * 实际卡牌效果
     */
    abstract void cardEffective();

    public String getUserId() {
        return userId;
    }

    public CardEvent userId(String userId) {
        this.userId = userId;
        return this;
    }

    public String getCardName() {
        return cardName;
    }

    public CardEvent cardName(String cardName) {
        this.cardName = cardName;
        return this;
    }

    public boolean isAvailable() {
        return available;
    }

    public CardEvent available(boolean available) {
        this.available = available;
        LogUtil.println("%s使用的【%s】被无效了", userId, cardName);
        return this;
    }

    public String getTargetId() {
        return targetId;
    }

    public CardEvent targetId(String targetId) {
        this.targetId = targetId;
        return this;
    }
}
